/*
 *所有关于PROC_MATERIAL类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*PROC_MATERIALService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.System.IRepositories;
using System.Collections.Generic;
using System;

namespace VOL.System.Services
{
    public partial class PROC_MATERIALService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IPROC_MATERIALRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public PROC_MATERIALService(
            IPROC_MATERIALRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        public override PageGridData<PROC_MATERIAL> GetPageData(PageDataOptions options)
        {
            QueryRelativeExpression = query =>
            {
              
                if (options.Value!=null)
                {
                    var value = options.Value.ToString();
                    query = query.Where(x => x.MaterialType.Contains(value));
                }
                else
                {
                    //物料
                    query = query.Where(x => x.MaterialType.Contains("0"));
                }
              
                return query;
            };




            var lists = base.GetPageData(options);
            lists.rows.ForEach(x =>
            {
              //  x.Img =string.Format( @$"/Upload/Tables/Sys_Dictionary/Img/{0}.jpg",x.);
                /*"http://192.168.28.181:3000/Upload/Tables/Sys_Dictionary/202410240857384858/Holder.jpg";*/
            });
            return lists;
        }
        //public override PageGridData<PLAN_WORK_ORDER> GetPageData(PageDataOptions options)
        //{
        //    var lists = base.GetPageData(options);
        //    lists.rows.ForEach(x =>
        //    {
        //        x.Img = "http://192.168.28.181:3000/Upload/Tables/Sys_Dictionary/202410240857384858/Holder.jpg";
        //    });
        //    return lists;
        //}

        public override WebResponseContent Update(SaveModel saveModel)
        {
            KeyValuePair<string, object> sHELVES_Id = saveModel.MainData.Where(t => t.Key == "SHELVES_Id").FirstOrDefault();
            KeyValuePair<string, object> location = saveModel.MainData.Where(t => t.Key == "location").FirstOrDefault();
            KeyValuePair<string, object> id = saveModel.MainData.Where(t => t.Key == "Id").FirstOrDefault();
            KeyValuePair<string, object> weight = saveModel.MainData.Where(t => t.Key == "Weight").FirstOrDefault();
            if (sHELVES_Id.Key != null)
            {
               
                var idStr = Guid.Parse(id.Value.ToString());
                if (sHELVES_Id.Value != "")
                {
                    var shelvesId = Guid.Parse(sHELVES_Id.Value.ToString());
                    var iNTE_SHELVES = _repository.DbContext.Set<INTE_SHELVES>()
                        .Where(t => t.INTE_SHELVES_Id == shelvesId).FirstOrDefault();
                    var pROC_MATERIAL = _repository.DbContext.Set<PROC_MATERIAL>().Where(t => t.Id == idStr).FirstOrDefault();
                    var pLAN_WORK= _repository.DbContext.Set<PLAN_WORK_ORDER>().Where(t => t.Order_Id ==Guid.Parse(pROC_MATERIAL.Order_Id)).FirstOrDefault();
                    if (pLAN_WORK != null)
                    {
                        pLAN_WORK.ProcessWeight = string.IsNullOrEmpty(weight.Value.ToString()) ? 0 : decimal.Parse(weight.Value.ToString());
                        _repository.DbContext.Set<PLAN_WORK_ORDER>().Update(pLAN_WORK);
                    }

                    iNTE_SHELVES.ToolId = pROC_MATERIAL.Id.ToString().ToUpper();
                    _repository.DbContext.Set<INTE_SHELVES>().Update(iNTE_SHELVES);
                    if (location.Key == null)
                    {
                        saveModel.MainData.Add("location", iNTE_SHELVES.DBCode);
                    }
                    else
                    {
                        saveModel.MainData["location"] = iNTE_SHELVES.DBCode;
                    }
                    var pROC_CALL_MATERIAL = _repository.DbContext.Set<PROC_CALL_MATERIAL>().Where(t => t.WorkpieceCode == pROC_MATERIAL.workpiece_name).FirstOrDefault();
                    if (pROC_CALL_MATERIAL != null)
                    {
                        pROC_CALL_MATERIAL.PlanStart = pLAN_WORK.PlanStart;
                        pROC_CALL_MATERIAL.PlanEnd = pLAN_WORK.PlanEnd;
                        pROC_CALL_MATERIAL.StorageState = "入库";
                        pROC_CALL_MATERIAL.SHELVES_Id = sHELVES_Id.Value.ToString();
                        _repository.DbContext.Set<PROC_CALL_MATERIAL>().Update(pROC_CALL_MATERIAL);
                    }
                    var iNTE_WAREHOUSE=_repository.DbContext.Set<INTE_WAREHOUSE>().Where(t => t.WarehouseCode == "A001").FirstOrDefault();
                    var wH_MATERIAL_STOCK = _repository.DbContext.Set<WH_MATERIAL_STOCK>().Where(t => t.MaterialId == pROC_MATERIAL.Id.ToString().ToUpper()).FirstOrDefault();
                    if(wH_MATERIAL_STOCK != null)
                    {
                        wH_MATERIAL_STOCK.Qty = 1;
                        if(iNTE_WAREHOUSE != null)
                        {
                            wH_MATERIAL_STOCK.WarehouseId = iNTE_WAREHOUSE.Id.ToString().ToUpper();
                        }
                        _repository.DbContext.Set<WH_MATERIAL_STOCK>().Update(wH_MATERIAL_STOCK);
                    }
                    else
                    {
                        wH_MATERIAL_STOCK = new WH_MATERIAL_STOCK();


                        wH_MATERIAL_STOCK.MaterialId = pROC_MATERIAL.Id.ToString().ToUpper();
                        wH_MATERIAL_STOCK.Qty = 1;
                        if (iNTE_WAREHOUSE != null)
                        {
                            wH_MATERIAL_STOCK.WarehouseId = iNTE_WAREHOUSE.Id.ToString().ToUpper();
                        }
                        _repository.DbContext.Set<WH_MATERIAL_STOCK>().Add(wH_MATERIAL_STOCK);
                    }
                }
                else
                {
                    var pROC_MATERIAL = _repository.DbContext.Set<PROC_MATERIAL>().Where(t => t.Id == idStr).FirstOrDefault();
                    var pROC_CALL_MATERIAL = _repository.DbContext.Set<PROC_CALL_MATERIAL>().Where(t => t.WorkpieceCode == pROC_MATERIAL.workpiece_name).FirstOrDefault();
                    if (pROC_CALL_MATERIAL != null)
                    {
                        pROC_CALL_MATERIAL.StorageState = "未入库";
                        pROC_CALL_MATERIAL.SHELVES_Id = null;
                        _repository.DbContext.Set<PROC_CALL_MATERIAL>().Update(pROC_CALL_MATERIAL);
                    }
                    var order_Id= pROC_MATERIAL.Order_Id != null?pROC_MATERIAL.Order_Id:Guid.Empty.ToString(); 
                    var pLAN_WORK = _repository.DbContext.Set<PLAN_WORK_ORDER>().Where(t => t.Order_Id == Guid.Parse(order_Id)).FirstOrDefault();
                    
                    if(pLAN_WORK!=null)
                    {
                        pLAN_WORK.ProcessWeight = string.IsNullOrEmpty(weight.Value.ToString()) ? 0 : decimal.Parse(weight.Value.ToString());
                        _repository.DbContext.Set<PLAN_WORK_ORDER>().Update(pLAN_WORK);
                    }
                }
            }
            return base.Update(saveModel);
        }
    }
}
